<?php

namespace App\Mail;

use App\Models\Article\ArticleInfo;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Str;

class OrderShipped extends Mailable
{
    use Queueable, SerializesModels;

    public $sender;
    public $title;
    public $article;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($articleId)
    {
        $this->sender = env('MAIL_FROM_NAME','池塘边素材');
        $this->article = ArticleInfo::query()->find($articleId);
        $this->title = empty($this->article)?'未找到文章':$this->article->title;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        if($this->article->type=='pptx'){
            $email = $this->view('articleEmail')->subject($this->title)
                ->attach(public_path($this->article->file_url),
                    ['as' => $this->title]);
            if(!empty($this->article->docx_url)){
                $email = $email->attach(public_path($this->article->docx_url),
                    ['as'=>'演讲稿-'.Str::replace('pptx', 'docx', $this->title)]);
            }

        }
        elseif ($this->article->type=='docx'){
            $email = $this->view('articleEmail')->subject($this->title)
                ->attach(public_path($this->article->file_url),
                    ['as' => $this->title]);
        }
        else{
            $email = $this->view('articleEmail')->subject($this->title);
        }

        return $email;


    }
}
